package com.starlink.controller.admin;

import com.github.pagehelper.PageInfo;
import com.starlink.common.WebResponse;
import com.starlink.common.annotation.WebAccess;
import com.starlink.model.request.MallPeripheryRequest;
import com.starlink.model.request.save.MallPeripherySaveRequest;
import com.starlink.model.vo.MallPeripheryVO;
import com.starlink.service.MallPeripheryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Auther: zhouyuhang01
 * @Date: 2024/4/10 16:28
 * @Description: AdminMallPeripheryController
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/admin/periphery")
@Api(value = "【管理端】商城文创周边模块", tags = {"【管理端】商城文创周边模块"})
public class AdminMallPeripheryController {

    @Autowired
    private MallPeripheryService peripheryService;

    @WebAccess(login = true)
    @ApiOperation(value = "查询周边商品详情", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/get/{id}")
    public WebResponse<MallPeripheryVO> getByKey(@PathVariable("id") long id) {
        return WebResponse.build(peripheryService.getByKey(id));
    }

    @WebAccess(login = true)
    @ApiOperation("周边商品列表分页查询")
    @PostMapping("/pagingQuery")
    public WebResponse<PageInfo<List<MallPeripheryVO>>> pagingQuery(@SortDefault(sort = {"create_time"}, direction = Sort.Direction.DESC) Pageable pageable,
                                                                @RequestBody MallPeripherySaveRequest request) {
        return WebResponse.build(peripheryService.pageQuery(pageable,request));
    }

    @WebAccess(login = true)
    @ApiOperation("周边商品编辑新增")
    @PostMapping("/add")
    public WebResponse<Boolean> add(@Valid @RequestBody MallPeripherySaveRequest request) {
        return WebResponse.build(peripheryService.insertOrUpdateDO(request));
    }

    @WebAccess(login = true)
    @ApiOperation("上架或下架修改")
    @PostMapping("/update")
    public WebResponse<Boolean> updateByKey(@Valid @RequestBody MallPeripheryRequest request) {
        return WebResponse.build(peripheryService.updateByKey(request));
    }

    @WebAccess(login = true)
    @ApiOperation("删除")
    @PostMapping("/detail/delete")
    public WebResponse<Boolean> deleteDetailByKey(@RequestParam String ids) {
        return WebResponse.build(peripheryService.deleteDOByIds(ids));
    }
}
